﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace FirstPerson
{
    public class WayPoint
    {
        private int identifier;
        /// <summary>
        /// There is a path between the neighbors and they are used to know where
        /// you can go from this waypoint.
        /// adjacentWaypoints are used in the xml file and are translated to actual
        /// waypoints that is placed in the neighbors list
        /// </summary>
        private int[] adjacentWaypoints;
        private List<WayPoint> neighbors= new List<WayPoint>();

        public List<WayPoint> Neighbors
        {
            get { return neighbors; }
            set { neighbors = value; }
        }
        private Vector2 position;
        /// <summary>
        /// This is the point that this waypoint is pointing at.
        /// this means that if you follow the parentnodes you will get back to the startnode
        /// </summary>
        private WayPoint parentPoint;

        public WayPoint ParentPoint
        {
            get { return parentPoint; }
            set { parentPoint = value; }
        }
        /// <summary>
        /// G = the movement cost to move from the starting point A to a given square on the grid, following the path generated to get there.
        /// H = the estimated movement cost to move from that given square on the grid to the final destination, point B.
        /// F is calculated by adding G and H and is seem like the total cost
        /// 
        /// </summary>
        private float f, g, h;

        public float H
        {
            get { return h; }
            set { h = value; }
        }

        public float G
        {
            get { return g; }
            set { g = value; }
        }

        public float F
        {
            get { return f; }
            set { f = value; }
        }

        public Vector2 Position
        {
            get { return this.position; }
            set { this.position = value; }
        }

        public int ID
        {
            get { return this.identifier; }
            set { this.identifier = value; }
        }

        public int[] AdjacentWaypoints
        {
            get { return this.adjacentWaypoints; }
            set { this.adjacentWaypoints = value; }
        }


        /// <summary>
        /// calculates the values that are needed for the A* algorithm.
        /// H is the esimated distance from this node to the goalnode.
        /// G is the cost from this node back to the goalnode if you follow the calculated path
        /// F is the total of H+G
        /// </summary>
        /// <param name="goal"></param>
        public void CalcGFH(WayPoint goal)
        {
            int tot;
            int xx =(int) Math.Abs(this.Position.X - goal.position.X);
            int yy = (int)Math.Abs(this.position.Y - goal.position.Y);

            tot = xx + yy;
            H = (tot * 10);
            calcG();
            F = H + G;

        }
        /// <summary>
        /// Calculates the G by taking the parents G and adding the Cost
        /// </summary>
        private void calcG()
        {
            if (ParentPoint != null)
                G = ParentPoint.G + 10;
            else
                G = 10;
        }
    }
}
